/**
 * @Author: LiMeng
 * @Date: 2017/5/13
 */
var add_checkInfo_func = function () {
    WindowManager.openLayer({
        type: 2,
        title: '添加检查信息',
        area: ['1150px', '70%'],
        content: ROOT_PATH + '/securityCheck/checkInfo/add.shtml',
        callBack: function (data) {
            if (data) {
                $('#dg1').datagrid('load');
            }
        }
    });
};
var edit_checkInfo_func = function () {
    var $selected = $('#dg1').datagrid('getSelections');
    if ($selected.length !== 1) {
        layer.alert('请选择一个检查信息！');
        return false;
    }
    WindowManager.openLayer({
        type: 2,
        title: '编辑检查信息',
        area: ['1150px', '70%'],
        content: ROOT_PATH + '/securityCheck/checkInfo/edit.shtml?id=' + $selected[0].id,
        callBack: function (data) {
            if (data) {
                $('#dg1').datagrid('load');
            }
        }
    });
};
var delete_checkInfo_func = function () {
    var $selected = $('#dg1').datagrid('getSelections');
    if ($selected.length !== 1) {
        layer.alert('请选择一个检查信息！');
        return false;
    }
    var index = parent.layer.confirm('是否确定删除？', function () {
        $.ajax({
            type: "POST",
            url: ROOT_PATH + '/securityCheck/checkInfo/delete.shtml',
            data: {id: $selected[0].id},
            success: function (result) {
                result = $.parseJSON(result);
                if (result.success) {
                    parent.layer.msg(result.message, {time: 1000, icon: 1}, function () {
                        $('#dg1').datagrid('load');
                        parent.layer.close(index); //先得到当前iframe层的索引
                    });
                } else {
                    parent.layer.msg(result.message);
                }
            }
        });
    });
};
var view_checkInfo_func = function (id) {
    WindowManager.openLayer({
        type: 2,
        title: '查看检查信息',
        area: ['1150px', '70%'],
        content: ROOT_PATH + '/securityCheck/checkInfo/view.shtml?id=' + id
    });
};
var getSelectedAddUser = function (data) {
    var names = '';
    var ids = '';
    for (var index in data) {
        names += ',' + data[index].realName;
        ids += ',' + data[index].id;
    }

    $('#addUserName').textbox('setValue', names.substring(1));
    $('#addUserId').val(ids.substring(1));
};
var getSelectedCheckUser = function (data) {
    var names = '';
    var ids = '';
    for (var index in data) {
        names += ',' + data[index].realName;
        ids += ',' + data[index].id;
    }

    $('#checkUserName').textbox('setValue', names.substring(1));
    $('#checkUserId').val(ids.substring(1));
};
var openSelectUser = function (title, callback) {
    WindowManager.openLayer({
        type: 2,
        title: title,
        area: ['900px', '500px'],
        content: ROOT_PATH + '/permission/user/simpleList.shtml',
        callBack: callback
    });
};
$(function () {
    $('#checkTypeId').combobox({
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml?code=' + CONSTANT.DictionaryTypeDefConstants.CHECK_TYPE,
        valueField: 'id',
        editable: false
    });

    $('#checkLevelId').combobox({
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml?code=' + CONSTANT.DictionaryTypeDefConstants.CHECK_LEVEL,
        valueField: 'id',
        editable: false
    });

    $('#checkUserName').textbox({
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                openSelectUser('选择检查人', getSelectedCheckUser);
            }
        }],
        editable: false
    });

    $('#checkDeptId').combotree({
        url: ROOT_PATH + '/permission/department/getDepartmentTree.shtml?parentId=0',
        editable: false
    });

    $('#addUserName').textbox({
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                openSelectUser('选择登记人', getSelectedAddUser);
            }
        }],
        editable: false
    });

    $('#addDeptId').combotree({
        url: ROOT_PATH + '/permission/department/getDepartmentTree.shtml?parentId=0',
        editable: false
    });
    //初始化加载datagrid
    $('#dg1').datagrid({
        method: 'post',
        url: ROOT_PATH + '/securityCheck/checkInfo/list.shtml',
        queryParams: {
            sort: 'addTime',
            order: 'desc'
        },
        toolbar: '#toolbar',
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        sortOrder: true,
        striped: true,
        fit: true,
        columns: [[
            {field: 'ckid', checkbox: true},
            {
                field: 'name', title: '检查名称', width: 200, formatter: function (value, row) {
                return '<a href="javascript:void(0)" onclick="view_checkInfo_func(\'' + row.id + '\')" class="easyui-linkbutton text-primary" data-options="plain:true">' + value + '</a>';
            }
            },
            {field: 'checkTypeName', title: '检查类型', width: 120},
            {field: 'checkLevelName', title: '检查级别', width: 100},
            {field: 'checkUserNames', title: '检查人', width: 100},
            {field: 'checkDeptNames', title: '检查部门', width: 120},
            {field: 'checkStartDate', title: '检查开始日期', width: 100},
            {field: 'checkEndDate', title: '检查结束日期', width: 100},
            {field: 'addUserId', title: '登记人id', hidden: true},
            {field: 'addUserName', title: '登记人', width: 60},
            {field: 'addDeptId', title: '登记部门id', hidden: true},
            {field: 'addDeptName', title: '登记部门', width: 100},
            {field: 'addTime', title: '登记时间', width: 100, sortable: true}
        ]]
    });
    $('#btn_clear').on("click", function () {
        $('#tb1').form('reset');
        $('#tb2').form('reset').find(':hidden').val('');
    });
    $('#btn_search').on("click", function () {
        $('#dg1').datagrid('reload', $('#tb1,#tb2').find(':input').serializeJson());
    });
    $('#btn_advanced_search').on("click", function () {
        $('#tb2').toggle();
        $('#dg1').datagrid('resize');
    });
    $('#btn_checkInfo_add').on('click', function () {
        add_checkInfo_func();
    });
    $('#btn_checkInfo_edit').on('click', function () {
        edit_checkInfo_func();
    });
    $('#btn_checkInfo_delete').on('click', function () {
        delete_checkInfo_func();
    });
});